package com.cs307.redcross.client;

import java.io.Serializable;
import java.util.Vector;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ChangeEvent;

public class TemplatesReminderContent extends Content implements Serializable {
	private static final long serialVersionUID = -1189011235559691201L;
	private DatabaseHandlerServiceAsync databaseHandlerService = (DatabaseHandlerServiceAsync) GWT.create(DatabaseHandlerService.class);
	private final String relogString = "If this problem persists try relogging into the system.";
	
	Vector<ReminderTemplate> ReminderTemplateVector = new Vector<ReminderTemplate>();
	
	ReminderTemplate cancelReminderTemplate;
	ReminderTemplate selectedReminderTemplate;
	ReminderTemplate reminderTemplateCopy;
	
	// UI Widgets
	ListBox reminderTemplateListbox;
	TextBox reminderTemplateNameTextbox;
	TextArea messageTextBox;
	Button addEventButton;
	Button deleteEventButton;
	Button editEventButton;
	Button saveReminderTemplateButton;
	Button cancelButton;
	PopupPanel popup;
	AddGroupPopupPanel agpp;
	AddPersonPopupPanel appp;
	ListBox daysBeforeCombobox;
	ListBox minutesBeforeCombobox;
	ListBox hoursBeforeCombobox;
	ListBox deliveryMethodCombobox;
	
	public TemplatesReminderContent() {
		
		ContentPanel.getInstance().showLoading(1, 80, 350);
		databaseHandlerService.getAllReminderTemplates(new AsyncCallback<Vector<ReminderTemplate>>() {
			
			@Override
			public void onSuccess(Vector<ReminderTemplate> result) {
				if (result != null && !result.isEmpty()) {
					for (ReminderTemplate event : result) {
						if (event == null)
							continue;
						
						ReminderTemplateVector.add(event);
						reminderTemplateListbox.addItem(event.getName());
					}
					ContentPanel.getInstance().hideLoadingPopup(1);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				ContentPanel.getInstance().hideLoadingPopup(1);
				Window.alert("Error: Failed to retrieve list of Reminder Templates. Please try reloading the page. " + relogString);
				return;
			}
		});	
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("1121px", "653px");
		
		Label lblSelectEvent = new Label("Reminder Templates:");
		absolutePanel.add(lblSelectEvent, 10, 10);
		
		Label reminderTemplateName = new Label("Reminder Template Name:");
		absolutePanel.add(reminderTemplateName, 319, 10);
		reminderTemplateName.setSize("158px", "18px");
		
		reminderTemplateNameTextbox = new TextBox();
		absolutePanel.add(reminderTemplateNameTextbox, 319, 34);
		reminderTemplateNameTextbox.setSize("364px", "13px");
		
		messageTextBox = new TextArea();
		absolutePanel.add(messageTextBox, 319, 139);
		messageTextBox.setSize("748px", "455px");
		
		Label lblEventDescription = new Label("Message:");
		absolutePanel.add(lblEventDescription, 319, 115);
		lblEventDescription.setSize("122px", "18px");
		
		addEventButton = new Button("Add");
		addEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				clearForm();
				
				reminderTemplateListbox.setSelectedIndex(-1);
				selectedReminderTemplate = null;
				reminderTemplateNameTextbox.setText("New Reminder Template");
				
				enableForm();
				reminderTemplateListbox.setEnabled(false);
			}
		});
		absolutePanel.add(addEventButton, 10, 604);
		
		deleteEventButton = new Button("Delete");
		deleteEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (selectedReminderTemplate != null && reminderTemplateListbox.getSelectedIndex() != -1) {
					popup = new PopupPanel();
					popup.setPopupPosition(400, 400);
					VerticalPanel vp = new VerticalPanel();
					vp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
					vp.add(new Label("Are you sure you want to delete " + selectedReminderTemplate.getName() + "? You cannot undo this action."));
					
					Button yesButton = new Button("Yes");
					yesButton.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							popup.hide();
							popup.clear();
							databaseHandlerService.delete(selectedReminderTemplate, new AsyncCallback<Boolean>() {
								
								@Override
								public void onSuccess(Boolean result) {
									if (result) {
										ReminderTemplateVector.remove(reminderTemplateListbox.getSelectedIndex());
										reminderTemplateListbox.removeItem(reminderTemplateListbox.getSelectedIndex());
										selectedReminderTemplate = null;
										clearForm();
										disableForm();
										Window.alert("Reminder Template successfully deleted.");
									}
									else {
										Window.alert("An error ocurred while trying to delete the Reminder Template. Please try again. " + relogString);
									}
								}
								
								@Override
								public void onFailure(Throwable caught) {
									Window.alert("An error ocurred while trying to delete the Reminder Template. Please try again. " + relogString);
								}
							});
						}
					});
					
					Button noButton = new Button("No");
					noButton.addClickHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							popup.hide();
							popup.clear();
						}
					});
					
					SimplePanel sp = new SimplePanel();
					sp.setSize("5px", "20px");
					
					HorizontalPanel hp = new HorizontalPanel();
					hp.add(yesButton);
					hp.add(new Label("         "));
					hp.add(noButton);
					
					vp.add(sp);
					vp.add(hp);
					popup.add(vp);
					popup.show();
				}
			}
		});
		absolutePanel.add(deleteEventButton, 54, 604);
		deleteEventButton.setSize("53px", "24px");
		
		editEventButton = new Button("Edit");
		editEventButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (reminderTemplateListbox.getSelectedIndex() != -1) {
					enableForm();
					reminderTemplateListbox.setEnabled(false);
				}
			}
		});
		absolutePanel.add(editEventButton, 113, 604);
		editEventButton.setSize("38px", "24px");
		
		reminderTemplateListbox = new ListBox();
		reminderTemplateListbox.addChangeHandler(new ChangeHandler() {
			public void onChange(ChangeEvent event) {
				populateForm();
				if (selectedReminderTemplate != null) {
					cancelReminderTemplate = new ReminderTemplate(selectedReminderTemplate);
				}
			}
		});
		reminderTemplateListbox.setVisibleItemCount(30);
		absolutePanel.add(reminderTemplateListbox, 10, 34);
		reminderTemplateListbox.setSize("259px", "564px");
		
		saveReminderTemplateButton = new Button("Save Event");
		saveReminderTemplateButton.setText("Save");
		saveReminderTemplateButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
								
				if (reminderTemplateNameTextbox.getText().replace(" ","").equals("")) {
					Window.alert("The Template has no name!");
					return;
				}
				if (messageTextBox.getText().replace(" ","").equals("")) {
					Window.alert("The Template has no message!");
					return;
				}

				if (selectedReminderTemplate != null) {
					reminderTemplateCopy = new ReminderTemplate(selectedReminderTemplate);
				}
				else {
					selectedReminderTemplate = new ReminderTemplate();
					reminderTemplateCopy = null;
				}
								
				selectedReminderTemplate.setName(reminderTemplateNameTextbox.getText());
				selectedReminderTemplate.setMessage(messageTextBox.getText());
				selectedReminderTemplate.setTimeBeforeEvent(daysBeforeCombobox.getSelectedIndex() * 1440 + hoursBeforeCombobox.getSelectedIndex() * 60 + minutesBeforeCombobox.getSelectedIndex());
				selectedReminderTemplate.setGroups(new Vector<Group>());
				selectedReminderTemplate.setPeople(new Vector<Person>());
				
				if (deliveryMethodCombobox.getSelectedIndex() == 0) {
					selectedReminderTemplate.setEmail(true);
					selectedReminderTemplate.setSMS(true);
				}
				else if (deliveryMethodCombobox.getSelectedIndex() == 1) {
					selectedReminderTemplate.setEmail(true);
					selectedReminderTemplate.setSMS(false);
				}
				else {
					selectedReminderTemplate.setEmail(false);
					selectedReminderTemplate.setSMS(true);
				}
				
				databaseHandlerService.write(selectedReminderTemplate, new AsyncCallback<Integer>() {

					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Error: Failed to save the Reminder Template. " + relogString + "\n\n" + caught.toString());
						if (reminderTemplateCopy != null) {
							selectedReminderTemplate = new ReminderTemplate(reminderTemplateCopy);
						}
					}

					@Override
					public void onSuccess(Integer result) {
						if (result > -1) {
							if (selectedReminderTemplate.getId() < 0)
								selectedReminderTemplate.setId(result);
														
							if (reminderTemplateCopy == null) {
								reminderTemplateListbox.addItem(selectedReminderTemplate.getName());
								ReminderTemplateVector.add(selectedReminderTemplate);
								
								if (reminderTemplateListbox.getItemCount() > 0) 
									reminderTemplateListbox.setSelectedIndex(ReminderTemplateVector.size() - 1);
							}
							
							disableForm();
							reminderTemplateListbox.setEnabled(true);
							Window.alert("The Reminder Template was saved succesfully.");
						}
						else {
							Window.alert("Error: Failed to save the Reminder Template. " + relogString);
							if (reminderTemplateCopy != null) {
								selectedReminderTemplate = new ReminderTemplate(reminderTemplateCopy);
							}
						}
					}
				});
			}
		});
		absolutePanel.add(saveReminderTemplateButton, 10, 604);
		saveReminderTemplateButton.setSize("97px", "24px");
		
		cancelButton = new Button("Cancel");
		cancelButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				if (cancelReminderTemplate != null) {
					selectedReminderTemplate = new ReminderTemplate(cancelReminderTemplate);
				}
					
				if (selectedReminderTemplate != null && reminderTemplateListbox.getSelectedIndex() != -1) {
					populateForm();
				}
				else {
					clearForm();
					disableForm();
				}
				reminderTemplateListbox.setEnabled(true);
			}
		});
		absolutePanel.add(cancelButton, 113, 604);
		cancelButton.setSize("97px", "24px");
		
		daysBeforeCombobox = new ListBox();
		daysBeforeCombobox.addItem("0");
		daysBeforeCombobox.addItem("1");
		daysBeforeCombobox.addItem("2");
		daysBeforeCombobox.addItem("3");
		daysBeforeCombobox.addItem("4");
		daysBeforeCombobox.addItem("5");
		daysBeforeCombobox.addItem("6");
		daysBeforeCombobox.addItem("7");
		daysBeforeCombobox.addItem("8");
		daysBeforeCombobox.addItem("9");
		daysBeforeCombobox.addItem("10");
		daysBeforeCombobox.addItem("11");
		daysBeforeCombobox.addItem("12");
		daysBeforeCombobox.addItem("13");
		daysBeforeCombobox.addItem("14");
		daysBeforeCombobox.addItem("15");
		daysBeforeCombobox.addItem("16");
		daysBeforeCombobox.addItem("17");
		daysBeforeCombobox.addItem("18");
		daysBeforeCombobox.addItem("19");
		daysBeforeCombobox.addItem("20");
		daysBeforeCombobox.addItem("21");
		daysBeforeCombobox.addItem("22");
		daysBeforeCombobox.addItem("23");
		daysBeforeCombobox.addItem("24");
		daysBeforeCombobox.addItem("25");
		daysBeforeCombobox.addItem("26");
		daysBeforeCombobox.addItem("27");
		daysBeforeCombobox.addItem("28");
		daysBeforeCombobox.addItem("29");
		daysBeforeCombobox.addItem("30");
		daysBeforeCombobox.addItem("31");
		daysBeforeCombobox.addItem("32");
		daysBeforeCombobox.addItem("33");
		daysBeforeCombobox.addItem("34");
		daysBeforeCombobox.addItem("35");
		daysBeforeCombobox.addItem("36");
		daysBeforeCombobox.addItem("37");
		daysBeforeCombobox.addItem("38");
		daysBeforeCombobox.addItem("39");
		daysBeforeCombobox.addItem("40");
		daysBeforeCombobox.addItem("41");
		daysBeforeCombobox.addItem("42");
		daysBeforeCombobox.addItem("43");
		daysBeforeCombobox.addItem("44");
		daysBeforeCombobox.addItem("45");
		daysBeforeCombobox.addItem("46");
		daysBeforeCombobox.addItem("47");
		daysBeforeCombobox.addItem("48");
		daysBeforeCombobox.addItem("49");
		daysBeforeCombobox.addItem("50");
		daysBeforeCombobox.addItem("51");
		daysBeforeCombobox.addItem("52");
		daysBeforeCombobox.addItem("53");
		daysBeforeCombobox.addItem("54");
		daysBeforeCombobox.addItem("55");
		daysBeforeCombobox.addItem("56");
		daysBeforeCombobox.addItem("57");
		daysBeforeCombobox.addItem("58");
		daysBeforeCombobox.addItem("59");
		daysBeforeCombobox.addItem("60");
		daysBeforeCombobox.addItem("61");
		daysBeforeCombobox.addItem("62");
		absolutePanel.add(daysBeforeCombobox, 319, 91);
		daysBeforeCombobox.setSize("50px", "18px");
		
		minutesBeforeCombobox = new ListBox();
		minutesBeforeCombobox.addItem("0");
		minutesBeforeCombobox.addItem("1");
		minutesBeforeCombobox.addItem("2");
		minutesBeforeCombobox.addItem("3");
		minutesBeforeCombobox.addItem("4");
		minutesBeforeCombobox.addItem("5");
		minutesBeforeCombobox.addItem("6");
		minutesBeforeCombobox.addItem("7");
		minutesBeforeCombobox.addItem("8");
		minutesBeforeCombobox.addItem("9");
		minutesBeforeCombobox.addItem("10");
		minutesBeforeCombobox.addItem("11");
		minutesBeforeCombobox.addItem("12");
		minutesBeforeCombobox.addItem("13");
		minutesBeforeCombobox.addItem("14");
		minutesBeforeCombobox.addItem("15");
		minutesBeforeCombobox.addItem("16");
		minutesBeforeCombobox.addItem("17");
		minutesBeforeCombobox.addItem("18");
		minutesBeforeCombobox.addItem("19");
		minutesBeforeCombobox.addItem("20");
		minutesBeforeCombobox.addItem("21");
		minutesBeforeCombobox.addItem("22");
		minutesBeforeCombobox.addItem("23");
		minutesBeforeCombobox.addItem("24");
		minutesBeforeCombobox.addItem("25");
		minutesBeforeCombobox.addItem("26");
		minutesBeforeCombobox.addItem("27");
		minutesBeforeCombobox.addItem("28");
		minutesBeforeCombobox.addItem("29");
		minutesBeforeCombobox.addItem("30");
		minutesBeforeCombobox.addItem("31");
		minutesBeforeCombobox.addItem("32");
		minutesBeforeCombobox.addItem("33");
		minutesBeforeCombobox.addItem("34");
		minutesBeforeCombobox.addItem("35");
		minutesBeforeCombobox.addItem("36");
		minutesBeforeCombobox.addItem("37");
		minutesBeforeCombobox.addItem("38");
		minutesBeforeCombobox.addItem("39");
		minutesBeforeCombobox.addItem("40");
		minutesBeforeCombobox.addItem("41");
		minutesBeforeCombobox.addItem("42");
		minutesBeforeCombobox.addItem("43");
		minutesBeforeCombobox.addItem("44");
		minutesBeforeCombobox.addItem("45");
		minutesBeforeCombobox.addItem("46");
		minutesBeforeCombobox.addItem("47");
		minutesBeforeCombobox.addItem("48");
		minutesBeforeCombobox.addItem("49");
		minutesBeforeCombobox.addItem("50");
		minutesBeforeCombobox.addItem("51");
		minutesBeforeCombobox.addItem("52");
		minutesBeforeCombobox.addItem("53");
		minutesBeforeCombobox.addItem("54");
		minutesBeforeCombobox.addItem("55");
		minutesBeforeCombobox.addItem("56");
		minutesBeforeCombobox.addItem("57");
		minutesBeforeCombobox.addItem("58");
		minutesBeforeCombobox.addItem("59");
		absolutePanel.add(minutesBeforeCombobox, 541, 91);
		minutesBeforeCombobox.setSize("50px", "18px");
		
		hoursBeforeCombobox = new ListBox();
		hoursBeforeCombobox.addItem("0");
		hoursBeforeCombobox.addItem("1");
		hoursBeforeCombobox.addItem("2");
		hoursBeforeCombobox.addItem("3");
		hoursBeforeCombobox.addItem("4");
		hoursBeforeCombobox.addItem("5");
		hoursBeforeCombobox.addItem("6");
		hoursBeforeCombobox.addItem("7");
		hoursBeforeCombobox.addItem("8");
		hoursBeforeCombobox.addItem("9");
		hoursBeforeCombobox.addItem("10");
		hoursBeforeCombobox.addItem("11");
		hoursBeforeCombobox.addItem("12");
		hoursBeforeCombobox.addItem("13");
		hoursBeforeCombobox.addItem("14");
		hoursBeforeCombobox.addItem("15");
		hoursBeforeCombobox.addItem("16");
		hoursBeforeCombobox.addItem("17");
		hoursBeforeCombobox.addItem("18");
		hoursBeforeCombobox.addItem("19");
		hoursBeforeCombobox.addItem("20");
		hoursBeforeCombobox.addItem("21");
		hoursBeforeCombobox.addItem("22");
		hoursBeforeCombobox.addItem("23");
		absolutePanel.add(hoursBeforeCombobox, 413, 91);
		hoursBeforeCombobox.setSize("53px", "18px");
		
		Label lblDays = new Label("days,");
		absolutePanel.add(lblDays, 375, 91);
		
		Label lblHours = new Label("hours, and");
		absolutePanel.add(lblHours, 472, 91);
		lblHours.setSize("65px", "18px");
		
		Label lblMinutesBeforeThe = new Label("minutes before the Event begins.");
		absolutePanel.add(lblMinutesBeforeThe, 597, 91);
		lblMinutesBeforeThe.setSize("209px", "18px");
		
		Label lblSendReminder = new Label("Send Reminder:");
		absolutePanel.add(lblSendReminder, 319, 61);
		lblSendReminder.setSize("97px", "18px");
		
		Label lblDeliveryMethod = new Label("Delivery Method:");
		absolutePanel.add(lblDeliveryMethod, 712, 10);
		
		deliveryMethodCombobox = new ListBox();
		deliveryMethodCombobox.addItem("Text + Email");
		deliveryMethodCombobox.addItem("Email Only");
		deliveryMethodCombobox.addItem("Text Only");
		absolutePanel.add(deliveryMethodCombobox, 712, 34);
		deliveryMethodCombobox.setSize("255px", "18px");
		
		disableForm();
	}
	
	private void populateForm() {
		
		if (reminderTemplateListbox.getSelectedIndex() != -1) {
			clearForm();
			selectedReminderTemplate = ReminderTemplateVector.get(reminderTemplateListbox.getSelectedIndex());
			
			if (selectedReminderTemplate != null) {
				reminderTemplateNameTextbox.setText(selectedReminderTemplate.getName());
				messageTextBox.setText(selectedReminderTemplate.getMessage());
				daysBeforeCombobox.setSelectedIndex(selectedReminderTemplate.getDaysBeforeEvent());
				hoursBeforeCombobox.setSelectedIndex(selectedReminderTemplate.getHoursBeforeEvent());
				minutesBeforeCombobox.setSelectedIndex(selectedReminderTemplate.getMinutesBeforeEvent());
			
				if (selectedReminderTemplate.email && selectedReminderTemplate.SMS) {
					deliveryMethodCombobox.setSelectedIndex(0);
				}
				else if (selectedReminderTemplate.email && !selectedReminderTemplate.SMS) {
					deliveryMethodCombobox.setSelectedIndex(1);
				}
				else {
					deliveryMethodCombobox.setSelectedIndex(2);
				}
			}
			
			disableForm();
			reminderTemplateListbox.setEnabled(true);
		}
	}
	
	private void disableForm() {
		reminderTemplateNameTextbox.setEnabled(false);
		messageTextBox.setEnabled(false);
		
		hoursBeforeCombobox.setEnabled(false);
		daysBeforeCombobox.setEnabled(false);
		minutesBeforeCombobox.setEnabled(false);
		
		addEventButton.setEnabled(true);
		editEventButton.setEnabled(true);
		deleteEventButton.setEnabled(true);
		
		daysBeforeCombobox.setEnabled(false);
		hoursBeforeCombobox.setEnabled(false);
		minutesBeforeCombobox.setEnabled(false);
		
		deliveryMethodCombobox.setEnabled(false);
				
		saveReminderTemplateButton.setVisible(false);
		cancelButton.setVisible(false);
	}
	
	private void enableForm() {
		reminderTemplateNameTextbox.setEnabled(true);
		messageTextBox.setEnabled(true);
		
		hoursBeforeCombobox.setEnabled(true);
		daysBeforeCombobox.setEnabled(true);
		minutesBeforeCombobox.setEnabled(true);
		
		addEventButton.setEnabled(false);
		editEventButton.setEnabled(false);
		deleteEventButton.setEnabled(false);
		
		daysBeforeCombobox.setEnabled(true);
		hoursBeforeCombobox.setEnabled(true);
		minutesBeforeCombobox.setEnabled(true);
		deliveryMethodCombobox.setEnabled(true);
		
		saveReminderTemplateButton.setVisible(true);
		cancelButton.setVisible(true);
	}
	
	private void clearForm() {
		reminderTemplateNameTextbox.setText("");
		messageTextBox.setText("");
		
		hoursBeforeCombobox.setItemSelected(0, false);
		daysBeforeCombobox.setItemSelected(0, false);
		minutesBeforeCombobox.setItemSelected(0, false);
	}
}
